package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;

    /**
     * 查询所有地址
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> listAddressBook(){
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getIsDeleted,0);
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping()
    public R addAddressBook(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        boolean save = addressBookService.save(addressBook);
        if(save){
            return R.success("新增地址成功");
        }
        return R.error("新增地址失败");
    }

    /**
     * 查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getAddressBookById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping()
    public R<String> putAddressBook(@RequestBody AddressBook addressBook){
        boolean save = addressBookService.updateById(addressBook);
        if(save){
            return R.success("更改地址成功");
        }
        return R.error("更改地址失败");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R<String> delAddressBook(Long ids){
        AddressBook addressBook=new AddressBook();
        addressBook.setId(ids);
        addressBook.setIsDeleted(1);
        boolean save = addressBookService.updateById(addressBook);
        if(save){
            return R.success("删除地址成功");
        }
        return R.error("删除地址失败");
    }


    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> defaultAddressBook(@RequestBody AddressBook addressBook){
       //先查当前用户下的所有地址
        LambdaUpdateWrapper<AddressBook> queryWrapper=new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.set(AddressBook::getIsDefault,0);
        //清空
        addressBookService.update(queryWrapper);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);

    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddressBook(){
        //先查当前用户下的所有地址
        LambdaUpdateWrapper<AddressBook> queryWrapper=new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        return R.success(addressBook);
    }


}
